package org.skyui.config;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

import org.skyui.annotations.SkyTable;
import org.skyui.beanmanagers.BeanSetManager;
import org.skyui.beans.OrganizationBean;
import org.skyui.views.AbstractBeansTableView.Action;

import com.baycrypt.server.beans.commands.DeviceLockCommandBean;
import com.baycrypt.server.beans.commands.DeviceUnlockCommandBean;
import com.baycrypt.server.beans.commands.WLANRotateKeyCommandBean;
import com.baycrypt.server.beans.devices.AccessPointBean;
import com.baycrypt.server.beans.devices.DeviceBean;
import com.baycrypt.server.beans.devices.WLANSwitchBean;
import com.baycrypt.server.beans.networks.WLANBean;
import com.baycrypt.server.beans.people.AccessCardSheetBean;
import com.baycrypt.server.beans.people.OneTimeLoginBean;
import com.baycrypt.server.beans.people.PendingUserBean;
import com.baycrypt.server.beans.people.UserBean;
import com.baycrypt.server.beans.people.UserGroupBean;
import com.baycrypt.server.beans.people.UserInvitationBean;

public class BeansTableViewConfig {

	protected String itemsCaption;
	protected String selectedItemCaption;
	protected String addItemCaption;
	protected String[] buttonNames;
	protected Action[] buttonActions;

	public BeansTableViewConfig(String itemsCaption,
			String selectedItemCaption, String addItemCaption,
			String[] buttonNames, Action[] actions) {
		this.itemsCaption = itemsCaption;
		this.selectedItemCaption = selectedItemCaption;
		this.addItemCaption = addItemCaption;
		this.buttonNames = buttonNames;
		this.buttonActions = actions;
	}

	static Map<Class, BeansTableViewConfig> configMap = new HashMap<Class, BeansTableViewConfig>();

	static {
		addToConfigMap(OrganizationBean.class);
	}

	public static BeansTableViewConfig addToConfigMap(Class cl) {
		SkyTable annotation = (SkyTable) cl.getAnnotation(SkyTable.class);
		if (annotation == null)
			throw new RuntimeException("Cant find @SkyTable in " + cl);
		BeansTableViewConfig btvc = new BeansTableViewConfig(annotation
				.itemsCaption(), annotation.selectedItemCaption(), annotation
				.addItemCaption(), annotation.buttonNames(), annotation
				.actions());
		configMap.put(cl, btvc);
		return btvc;
	}

	private String getItemsCaption() {
		return this.itemsCaption;
	}

	private String getSelectedItemCaption() {
		return this.selectedItemCaption;
	}

	private String getAddItemCaption() {
		return this.addItemCaption;
	}

	private String[] getButtonNames() {
		return this.buttonNames;
	}

	private Action[] getButtonActions() {
		return this.buttonActions;
	}

	public static String getItemsCaption(BeanSetManager manager) {
		return configureAndGet(manager.getBeanClass()).getItemsCaption();
	}

	public static String getSelectedItemCaption(BeanSetManager manager) {
		return configureAndGet(manager.getBeanClass()).getSelectedItemCaption();
	}

	public static String getAddItemCaption(BeanSetManager manager) {
		return configureAndGet(manager.getBeanClass()).getAddItemCaption();
	}

	public static String[] getButtonNames(BeanSetManager manager) {
		return configureAndGet(manager.getBeanClass()).getButtonNames();
	}

	public static Action[] getButtonActions(BeanSetManager manager) {
		return configureAndGet(manager.getBeanClass()).getButtonActions();
	}

	static BeansTableViewConfig configureAndGet(Class cl) {
		BeansTableViewConfig btvc = configMap.get(cl);
		if (btvc != null)
			return btvc;
		else
			return addToConfigMap(cl);
	}
};